// pages/goods_detail/goods_detail.js
import { request } from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj: {},
    // 商品是否被收藏过
    isCollect:false
  },
  //商品对象 
  GoodsInfo: {},
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let pages = getCurrentPages()
    console.log(pages, "11111");
    //2.数组索引最大的就是当前页面
    let currentPage = pages[pages.length - 1]
    // console.log(currentPage.options);
    let options = currentPage.options
    //获取商品Id
    const { goods_id } = options
    // console.log(goods_id);
    this.getGoodsDetail(goods_id)
  },
  //获取商品详情的数据
  async getGoodsDetail(goods_id) {
    const res = await request({ url: '/goods/detail', data: { goods_id } })
    console.log(res.data.message);
    let goodsObj = res.data.message
    this.GoodsInfo = goodsObj;
     // 1.获取缓存的商品数组 至少拿一个数组
     let collect = wx.getStorageSync("collect") || []
     // console.log(collect ,'dd');
     //2.判断商品是否被收藏了
     let isCollect = collect.some(v=>v.goods_id === this.GoodsInfo.goods_id)
    //保存数据
    this.setData({
      goodsObj: {
        pics: goodsObj.pics,
        goods_price: goodsObj.goods_price,
        goods_name: goodsObj.goods_name,
        //iphone 部分手机不识别webp图片格式
        //最好要后台改
        //临时自己改 webp 改成jpg
        goods_introduce: goodsObj.goods_introduce.replace(/\.webp/g, '.jpg')
      },
      isCollect
    })
  },
  //点击轮播图 放大预览handlePrevewImage
  handlePrevewImage(e) {
    // console.log('aa');
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid)
    //接收传过来的数据
    // console.log(e.currentTarget.dataset.url);
    const current = e.currentTarget.dataset.url
    //构造要预览的图片数组
    wx.previewImage({
      current,
      urls
    })
  },
  //点击加入购物车
  handleCartAdd() {
    //获取缓存中购物车 数组 
    let cart = wx.getStorageSync("cart") || [];
    console.log(cart);
    //判断 商品数据是否在购物车数组中
    //findIndex() 方法对数组中存在的每个元素执行一次函数
    let index = cart.findIndex(v => v.goods_id === this.GoodsInfo.goods_id);
    if (index === -1) {
      //不存在
      this.GoodsInfo.num = 1;
      this.GoodsInfo.checked = true;
      cart.push(this.GoodsInfo)
    } else {
      //已经存在购物车数据 执行num++
      cart[index].num++;
      console.log(cart[index].num);
    }
    // 5 把购物车重新添加回缓存中
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true
    })
  },
  //点击商品收藏
  handleCollect(){
    //获取缓存中商品数组
    let isCollect=false
    let collect =wx.getStorageSync('collect')||[]
    //判断该商品是否被收藏
    let index =collect.findIndex(v=>v.goods_id===this.GoodsInfo.goods_id)
    //当index=-1 表示已经收藏了
    if(index!==-1){
      //能找到 已经收藏过了 在数组中删除这个商品
      collect.splice(index,1)
      isCollect=false
      wx.showToast({
        title:'取消成功',
        icon:'success',
        mask:false
      })
    }else{
      //没有收藏
      collect.push(this.GoodsInfo)
      isCollect=true
      wx.showToast({
        title:'收藏成功',
        icon:'success',
        mask:false
      })
    }
    //把数组存储到缓存中
    wx.setStorageSync('collect',collect)
    //修改data的属性
    this.setData({
      isCollect
    })
  }
})